<?php
/**
 * Description
 *
 * PHP Version 5.6
 *
 * @category TelegramPostBot
 * @package  Data
 * @author   ctwj <908504609@qq.com>
 * @license  MIT https://mit-license.org/
 * @link     https://mit-license.org/
 * @date     2018-12-21 10:48:21
 * @modifyby ctwj
 */

namespace Telegram\Data;

/**
 * Message class
 *
 * 消息
 *
 * @category PHP
 * @package  Data
 * @author   ctwj <908504609@qq.com>
 * @license  MIT https://mit-license.org/
 * @link     MIT https://mit-license.org/
 */
class MessageModel extends BaseModel
{

    /**
     * 构造函数
     *
     * @param [type] $c Container
     *
     * @return void
     */
    public function __construct($c)
    {
        $this->table = $this->tableName('message');
        parent::__construct($c);
    }

    /**
     * 返回数据库中不存在的hash
     *
     * @param [array] $hashs hash数组
     *
     * @return array 不存在的hash
     */
    public function getNoExistsHashs($hashs)
    {
        $data = $this->find('hashkey', ['hashkey' => $hashs]);
        if (count($data) === 0) {
            return $hashs;
        }
        return array_diff($hashs, $data);
    }

    /**
     * 获取还没有post的数据
     *
     * @param [integer] $number 获取信息的数量
     * @param [array]   $order  排序顺序
     *
     * @return array of Telegram\Object\Message
     */
    public function getUnPostMessages($number = 5, $order = ["pub_date" => "DESC"])
    {
        $data = $this->instance->select(
            $this->table,
            [
                "[>]body"       => ["body_id" => "id"],
                "[>]resource"   => ["resource_id" => "id"],
            ],
            [
                'message.id','message.resource_id','message.hashkey','message.body_id',
                'message.message_url','message.title','message.abstract','message.pic',
                'message.thumbnail','message.pub_date','message.ispost','message.post_date',
                'message.author','message.category','message.vist_times[Int]',
                'message.tele_likes[Int]','message.tele_unlikes[Int]','message.ins_time[Int]',
                'body.content(content)','resource.extinfo(extinfo)[OBJECT]'],
            [
                'ispost'    => false,
                'ORDER'     => $order,
                'LIMIT'     => $number,
            ]
        );
        $this->checkError();
        return $data;
    }

    /**
     * 将数据保存到数据库
     *
     * @param [Telegram\Object\Message] $message 需要保存的消息对象
     *
     * @return integer
     */
    public function addMessage($message)
    {
        $data = $message->getArray();

        // 先添加 contents
        $content_id = getService('bodyModel')->add(['content' => $data['content']]);

        // 再添加 message
        $data['body_id'] = $content_id;
        unset($data['id']);
        unset($data['ins_time']);
        unset($data['content']);
        return $this->add($data);
    }

    /**
     * 跟新数据
     *
     * @param [Telegram\Object\Message] $message 需要保存的消息对象
     *
     * @return integer
     */
    public function updateMessage($message)
    {
        $data = $message->getArray();
        $id = $data['id'];

        unset($data['id']);
        unset($data['ins_time']);
        unset($data['content']);
        unset($data['body_id']);
        unset($data['resource_id']);
        unset($data['pub_date']);

        return $this->update($data, ['id' => $id]);
    }

    /**
     * 设置消息的状态为已推送 1
     *
     * @param [Array of Telegram\Object\Message] $messages messages
     *
     * @return array
     */
    private function _getMessageIds($messages)
    {
        $ids = [];
        foreach ($messages as $message) {
            $ids[] = $message->id;
        }
        return $ids;
    }

    /**
     * 设置消息的状态为已推送 1
     *
     * @param [type] $messages
     * @return integer
     */
    public function setMessagesPostSucc($messages)
    {
        $time = getTime()->format('Y-m-d H:i:s');
        $ids = $this->_getMessageIds($messages);
        return $this->update(['ispost' => 1, 'post_date' =>$time], ['id' => $ids]);
    }

    /**
     * 设置消息的状态为推送失败 2
     *
     * @param [type] $messages
     *
     * @return integer
     */
    public function setMessagesPostFail($messages)
    {
        $time = getTime()->format('Y-m-d H:i:s');
        $ids = $this->_getMessageIds($messages);
        return $this->update(['ispost' => 2, 'post_date' =>$time], ['id' => $ids]);
    }
}
